FROM haproxy:2.0.9

# set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

#RUN cat /etc/apt/sources.list
RUN sed -i -e 's/deb.debian.org/mirrors.163.com/g' /etc/apt/sources.list
RUN sed -i -e 's/security.debian.org/mirrors.163.com/g' /etc/apt/sources.list
RUN apt-get clean && apt-get update -y && apt-get upgrade -y && apt-get install -y gettext

# Keepalived version
#ARG KEEPALIVED_VERSION=2.0.19

RUN mkdir -p /data/keepalived
COPY keepalived-2.0.19.tar.gz /data/keepalived.tar.gz
# install keepalived
#RUN mkdir -p /data/keepalived && cd /data && apt-get install -y wget \
#    && wget -O keepalived.tar.gz -SL http://www.keepalived.org/software/keepalived-${KEEPALIVED_VERSION}.tar.gz \
RUN cd /data \
    && tar -xf keepalived.tar.gz -C keepalived --strip-components 1 \
    && cd keepalived && apt-get install -y gcc && apt-get install -y libssl-dev && apt-get -y install libpopt-dev \
    && ./configure && apt-get install -y make && make && make install

COPY keepalived/start_keepalived.sh /
RUN chmod +x /start_keepalived.sh

COPY keepalived/chk_haproxy.sh /etc/keepalived/chk_haproxy.sh
RUN chmod +x /etc/keepalived/chk_haproxy.sh

COPY keepalived/notify.sh /etc/keepalived/notify.sh
RUN chmod +x /etc/keepalived/notify.sh

COPY keepalived/keepalived.conf /etc/keepalived/keepalived.conf
COPY haproxy/haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

# Override haproxy's entrypoint
ENTRYPOINT ["/start_keepalived.sh"]

# CMD
CMD ["haproxy", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
